<?php
class Admin_master_controller extends CI_Controller {
	protected $view_data;
	protected $_language = "en";
	protected $_page_uri = "category";
	
	function __construct() {
		parent::__construct();
		$this->load->library('DX_Auth');
		// Protect entire controller so only admin, 
		// and users that have granted role in permissions table can access it.
		$this->dx_auth->check_uri_permissions();
		//$this->load->helper(array('url', 'date'));
		$this->load->library(array('validation'));
		$this->load->helper('htmlpurifier');
		//$this->view_data['languages'] = $this->lang->languages;
		//$this->view_data['lang_arr'] = $this->lang->lang_key();; 
		//$this->output->enable_profiler(TRUE);
		$this->view_data['active_category'] = "";
		$this->view_data['active_news'] = "";
		$this->view_data['active_product'] = "";
		$this->view_data['active_content'] = "";
		$this->view_data['active_config'] = "";
		$this->view_data['active_image'] = "";
		
		$this->load->library('session');
		$this->lang->load('admin', $this->session->userdata('language'));
		$_language = $this->session->userdata('language') == "english" ? "en" : "vi";
	}
	function render_page($data) {
		$this->load->view(ADMIN_PATH . 'master_admin_view', $data);
	}
	function en($page) {
		$this->session->set_userdata('language', 'english');
		redirect(admin_url().$page, 'refesh');
	}
	function vi($page) {
		$this->session->set_userdata('language', 'vietnamese');
		redirect(admin_url().$page, 'refesh');
	}
	function _resize($img, $width=200, $height=200) {
		$config['image_library'] = 'gd';
		$config['source_image'] = $img;
		$config['new_image'] = UPLOAD_PATH.'thumb/';
		$config['create_thumb'] = FALSE;
		$config['maintain_ratio'] = TRUE;
		$config['width'] = $width;
		$config['height'] = $height;
		$this->load->library('image_lib', $config);
		if(!$this->image_lib->resize())
			return false;
		else
			return true;
	}
	function _valid_date($str) {
		if(!ereg("^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[012])-([0-9]{4})$", $str)) {
			$this->validation->set_message('valid_date', 'date format is not valid. dd-mm-yyyy');
			return false;
		} else {
			return true;
		}
	}
	function _render_paging($count_all, $offset = 0) {
		$this->load->library('pagination');
		//Paging Config
 		$page_config['per_page'] = ADMIN_ITEM_PER_PAGE;
		$page_config['base_url'] = $this->_page_uri.'/index/';
 		$page_config['total_rows'] = $count_all;
		$page_config['cur_page'] = $offset;
		$page_config['first_link'] =$this->lang->line('first');
		$page_config['last_link'] =$this->lang->line('last');
		$this->pagination->initialize($page_config);
		return $this->pagination->create_links();
	}
}